package com.lemon.smartsafety.http.service;

import android.content.Context;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.entity.address.AddrBookApplyVo;
import com.lemon.smartsafety.entity.address.AddrBookInviteVo;
import com.lemon.smartsafety.entity.address.AddrBookUserVo;
import com.lemon.smartsafety.entity.address.AddrBookVo;
import com.lemon.smartsafety.entity.common.CommonVo;
import com.lemon.smartsafety.entity.common.IndexModel;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.ui.widget.CharacterParser;
import com.lemon.smartsafety.ui.widget.PinyinComparator;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.Collections;
import java.util.List;

/**
 * Created by Cooliean on 16/4/9.
 */
public class AddressBookServices extends BaseHttpServices {


    private final static int MAX_VALUE = 100;

    public AddressBookServices(Context context) {
        super(context);
    }

    /**
     * 初始化，等同使用构造函数
     *
     * @return
     */
    public static AddressBookServices init() {
        return new AddressBookServices(IApplication.context());
    }


    /**
     * 初始化，等同使用构造函数
     *
     * @param context
     * @return
     */
    public static AddressBookServices init(Context context) {
        return new AddressBookServices(context);
    }


    /**
     * 获取通讯录《通知公告选择用》
     */
    public void getAddressListForSelect() {
        LemonRequestParams params = new LemonRequestParams();
        params.put("current_page", "1");
        params.put("page_size", "" + MAX_VALUE);
        HandlerCallback<List<CommonVo>> callback = new HandlerCallback<List<CommonVo>>(EventTag.TAG_ADDRESS_BOOK_SELECT) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST_FOR_NOTICE);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);

    }


    /**
     * 获取通讯录《通讯录管理主界面》
     */
    public void getAddressList() {
        LemonRequestParams params = new LemonRequestParams();
        params.put("current_page", "1");
        params.put("page_size", "" + MAX_VALUE);
        params.put("op", "get_addr_book_list");
        params.put("addr_book_type", 1) ;
        HandlerCallback<List<AddrBookVo>> callback = new HandlerCallback<List<AddrBookVo>>(EventTag.TAG_ADDRESS_BOOK_LIST) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);

    }


    /**
     * 获取通讯录《通讯录管理主界面》 共享通讯录
     */
    public void getAddressCommon() {
        LemonRequestParams params = new LemonRequestParams();
        params.put("current_page", "1");
        params.put("page_size", "" + MAX_VALUE);
        params.put("op", "get_addr_book_list");
        params.put("addr_book_type", 2) ;
        HandlerCallback<List<AddrBookVo>> callback = new HandlerCallback<List<AddrBookVo>>(EventTag.TAG_ADDRESS_BOOK_COMMON) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);

    }


    /**
     * 获取通讯录《通讯录管理主界面》 系统通讯录
     */
    public void getAddressSystem() {
        LemonRequestParams params = new LemonRequestParams();
        params.put("current_page", "1");
        params.put("page_size", "" + MAX_VALUE);
        params.put("op", "get_addr_book_list");
        params.put("addr_book_type", 3) ;
        HandlerCallback<List<AddrBookVo>> callback = new HandlerCallback<List<AddrBookVo>>(EventTag.TAG_ADDRESS_BOOK_SYSTEM) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);

    }


    /**
     * 获取我的邀请
     */
    public void getInviteList() {
        LemonRequestParams params = new LemonRequestParams();
        params.put("current_page", "1");
        params.put("page_size", "" + MAX_VALUE);
        params.put("op", "get_my_invite_addr_book");
        HandlerCallback<List<AddrBookInviteVo>> callback = new HandlerCallback<List<AddrBookInviteVo>>(EventTag.TAG_ADDRESS_BOOK_MY_INVITE_LIST) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);

    }

    /**
     * 获取收到申请
     */
    public void getApplyList() {
        LemonRequestParams params = new LemonRequestParams();
        params.put("current_page", "1");
        params.put("page_size", "" + MAX_VALUE);
        params.put("op", "get_my_apply_addr_book");
        HandlerCallback<List<AddrBookApplyVo>> callback = new HandlerCallback<List<AddrBookApplyVo>>(EventTag.TAG_ADDRESS_BOOK_MY_APPLY_LIST) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);
    }


    /**
     * 获取通讯录成员列表
     *
     * @param book_id
     */
    public void getBookUserList(int book_id,int current_page) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("current_page", current_page);
        params.put("page_size", "20" );
        params.put("book_id", "" + book_id);
        params.put("op", "get_addr_book_user_list");
        HandlerCallback<List<AddrBookUserVo>> callback = new HandlerCallback<List<AddrBookUserVo>>(EventTag.TAG_ADDRESS_BOOK_CONTACT_LIST) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);
    }

    /**
     * 删除通讯录成员
     *
     * @param bookUserId
     */
    public void deleteContact(String bookUserId) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("book_user_id", "" + bookUserId);
        params.put("op", "delete_addr_book_user");
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_ADDRESS_BOOK_DELETE_CONTACT) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);
    }

    /**
     * 删除通讯录
     */
    public void deleteAddressBook(String bookId) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("book_id", "" + bookId);
        params.put("op", "delete_addr_book");
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_ADDRESS_BOOK_DELETE_ADDRESS_BOOK) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);
    }

    /**
     * 处理邀请
     *
     * @param bookUserId
     */
    public void handleInvite(String bookId, String bookUserId, String statusString) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("book_user_id", "" + bookUserId);
        params.put("response_status", "" + statusString);
        params.put("book_id", "" + bookId);
        params.put("op", "response_invite_addr_book");
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_ADDRESS_BOOK_INVITE_HANDLE) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);
    }

    /**
     * 处理申请
     */
    public void handleApply(String bookId, String applyUserId, String statusString) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("apply_user_id", "" + applyUserId);
        params.put("status", "" + statusString);
        params.put("book_id", "" + bookId);
        params.put("op", "response_apply_addr_book");
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_ADDRESS_BOOK_APPLY_HANDLE) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);
    }

    /**
     * 添加通讯录成员
     */
    public void saveAddressBook(String bookName, String keyword, String isPublic, String remark) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("book_name", "" + bookName);
        params.put("keywords", "" + keyword);
        params.put("is_public", "" + isPublic);
        params.put("remark", "" + remark);
        params.put("op", "new_addr_book");
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_ADDRESS_BOOK_SAVE) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);
    }

    /**
     * 添加通讯录成员
     */
    public void saveContract(String bookId, String userName, String mobile, String remark) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("user_name", "" + userName);
        params.put("mobile", "" + mobile);
        params.put("remark", "" + remark);
        params.put("book_id", "" + bookId);
        params.put("op", "new_addr_book_user");
        HandlerCallback<String> callback = new HandlerCallback<String>(EventTag.TAG_ADDRESS_BOOK_SAVE_CONTACT) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ADDRESS_BOOK_LIST);
        OkHttpUtils.getInstance().post().tag(callback.getTag()).url(serverUrl).params(params).build().execute(callback);
    }


    public static List sortData(List items) {
        PinyinComparator mPinyinComparator = new PinyinComparator();
        CharacterParser  mCharacterParser  = CharacterParser.getInstance();
        for (int i = 0; i < items.size(); i++) {
            IndexModel indexModel = (IndexModel) items.get(i);
            indexModel.setIndexChar(mCharacterParser.getSelling(indexModel.getTitleI()).substring(0, 1).toUpperCase());
        }
        Collections.sort(items, mPinyinComparator);
        return items;
    }


}
